package cn.t.service.order;

import cn.t.constants.CommonConstants;
import cn.t.core.service.ServiceImpl;
import cn.t.dto.api.ProductDTO;
import cn.t.dto.device.vo.QtyVO;
import cn.t.dto.sku.SkuDTO;
import cn.t.enums.order.WorkLineStatusEnum;
import cn.t.model.order.OrderDO;
import cn.t.model.order.OrderDetailDO;
import cn.t.model.order.WorkOrderDO;
import cn.t.model.order.WorkOrderDetailDO;
import cn.t.repository.order.WorkOrderDetailRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 工单明细表(WorkOrderDetail)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:12
 */
@Slf4j
@Service
@AllArgsConstructor
public class WorkOrderDetailService extends ServiceImpl<WorkOrderDetailDO, WorkOrderDetailRepository> {
    public Boolean updateBatchInfo(List<WorkOrderDetailDO> exceptionLogList){
        return repository.updateBatchList(exceptionLogList);
    }

    public List<WorkOrderDetailDO> getWorkOrderDetails(String skuCode, String skuName, List<String> workOrderSns) {
        WorkOrderDetailDO queryParam = new WorkOrderDetailDO();
        queryParam.setSkuCode(skuCode);
        queryParam.setSkuName(skuName);
        queryParam.setWorkOrderSns(workOrderSns);
        return this.findByModel(queryParam, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
    }

    public List<WorkOrderDetailDO> getWorkOrderDetails(String keyword) {
        WorkOrderDetailDO queryParam = new WorkOrderDetailDO();
        queryParam.setKeyword(keyword);
        return this.findByModel(queryParam, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
    }

    public Boolean addBatch(List<WorkOrderDetailDO> workOrderDetailDOS) {
        return this.repository.addBatch(workOrderDetailDOS);
    }
    public  void addDetailList(List<OrderDetailDO> orderDetailDOList, OrderDO orderDO) {
        List<WorkOrderDetailDO> list = orderDetailDOList.stream().map(orderDetailDO -> {
            WorkOrderDetailDO workOrderDetailDTO = new WorkOrderDetailDO();

            workOrderDetailDTO.setWorkOrderSn(orderDO.getWorkOrderSn());
            workOrderDetailDTO.setSkuCode(orderDetailDO.getSkuCode());
            workOrderDetailDTO.setSkuName(orderDetailDO.getSkuName());
            workOrderDetailDTO.setPlanQty(orderDetailDO.getQty());
            workOrderDetailDTO.setActQty(orderDetailDO.getQty());
            workOrderDetailDTO.setStatus(WorkLineStatusEnum.COMPLETED.getCode());
            workOrderDetailDTO.setRemark("系统创建");
            workOrderDetailDTO.setSkuImg(orderDetailDO.getSkuImg());
            workOrderDetailDTO.setCreator(orderDO.getCreator());
            workOrderDetailDTO.setModifier(orderDO.getModifier());
            return workOrderDetailDTO;
        }).collect(toList());
        addBatch(list);
    }
    public  void addDetailList(List<QtyVO> qtyVOList, Map<String, SkuDTO> skuDOMap, WorkOrderDO workOrderDO) {
        // 先对 qtyVOList 进行分组并求和
        Map<String, Integer> skuCodeToTotalQty = qtyVOList.stream()
                .collect(Collectors.groupingBy(QtyVO::getSkuCode, Collectors.summingInt(QtyVO::getQty)));

        List<WorkOrderDetailDO> details = new ArrayList<>();
        skuCodeToTotalQty.forEach((sku, qty)->{
            SkuDTO skuDtO = skuDOMap.get(sku);
            if (skuDtO != null){
                WorkOrderDetailDO workOrderDetailDTO = new WorkOrderDetailDO();

                workOrderDetailDTO.setWorkOrderSn(workOrderDO.getWorkOrderSn());
                workOrderDetailDTO.setSkuCode(skuDtO.getSkuCode());
                workOrderDetailDTO.setSkuName(skuDtO.getSkuName());
                workOrderDetailDTO.setPlanQty(qty);
                workOrderDetailDTO.setActQty(0);
                workOrderDetailDTO.setStatus(WorkLineStatusEnum.WAIT_OPERATE.getCode());
                workOrderDetailDTO.setRemark("系统创建");
                workOrderDetailDTO.setSkuImg(skuDtO.getSkuImg());
                workOrderDetailDTO.setCreator(workOrderDO.getCreator());
                workOrderDetailDTO.setModifier(workOrderDO.getModifier());
                details.add( workOrderDetailDTO);
            }

        });
        addBatch(details);
    }
    public Boolean conditionUpdate(String workOrderSn, WorkOrderDetailDO workOrderDetailDO) {
        return this.repository.conditionUpdate(workOrderSn, workOrderDetailDO);
    }
}
